package com.xudu.tourbackend.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.entity.Element;
import com.xudu.tourbackend.entity.Recommend;
import com.xudu.tourbackend.entity.Route;
import com.xudu.tourbackend.entity.vo.RouteVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.mapper.ElementMapper;
import com.xudu.tourbackend.mapper.RecommendMapper;
import com.xudu.tourbackend.mapper.RouteMapper;
import com.xudu.tourbackend.service.IElementService;
import com.xudu.tourbackend.service.IRecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xudu.tourbackend.service.IRouteService;
import com.xudu.tourbackend.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.xudu.tourbackend.constant.RecommendConstant.RECOMMENDID;
import static com.xudu.tourbackend.constant.RouteConstant.UNAPPROVED;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xudu
 * @since 2024-06-24
 */
@Service
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements IRecommendService {

    @Resource
    private RecommendMapper recommendMapper;

    @Resource
    private IRouteService routeService;

    @Resource
    private RouteMapper routeMapper;

    @Resource
    private IUserService userService;


    @Resource
    private IElementService elementService;

    @Resource
    private ElementMapper elementMapper;

    @Override
    public List<RouteVo> setRecommendRoute(List<Integer> ids, HttpServletRequest request) {
        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }

        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        List<Route> routeList = getRouteList(ids);

        Gson gson = new Gson();
        String recommends = gson.toJson(ids);
        Recommend recommend = new Recommend();
        recommend.setRecommendRoute(recommends);
        recommendMapper.insert(recommend);

        return routeService.getRouteInfoList(routeList);

    }

    @Override
    public List<RouteVo> getRecommendRoute() {
        String recommend = recommendMapper.selectById(1).getRecommendRoute();
        Gson gson = new Gson();
        //new TypeToken<List<Integer>>(){}.getType();
        List<Integer> list = gson.fromJson(recommend, new TypeToken<List<Integer>>() {}.getType());

        List<Route> routeList = getRouteList(list);

        return routeService.getRouteInfoList(routeList);

    }

    @Override
    public List<RouteVo> updateRecommendRoute(List<Integer> ids, HttpServletRequest request) {
        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }
        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        List<Route> routeList = getRouteList(ids);

        Gson gson = new Gson();
        String recommendsList = gson.toJson(ids);
        Recommend recommend = recommendMapper.selectById(RECOMMENDID);
        recommend.setRecommendRoute(recommendsList);
        recommendMapper.updateById(recommend);

        return routeService.getRouteInfoList(routeList);

    }

    @Override
    public List<Route> getRouteList(List<Integer> ids) {
        ArrayList<Route> routeList = new ArrayList<>();
        for (Integer id : ids){
            Route route = routeMapper.selectById(id);
            if (route == null || Objects.equals(route.getStatus(), UNAPPROVED)){
                throw new ServiceException(ErrorCode.PARAMS_ERROR);
            }
            routeList.add(route);
        }
        return routeList;
    }

    @Override
    public List<Element> getElement() {
        Recommend recommend = recommendMapper.selectById(RECOMMENDID);
        String recommendElement = recommend.getRecommendElement();
        if (recommendElement == null){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }
        Gson gson = new Gson();
        List<Integer> list = gson.fromJson(recommendElement, new TypeToken<List<Integer>>() {}.getType());

        return getElementList(list);
    }

    @Override
    public List<Element> updateElement(List<Integer> ids, HttpServletRequest request) {
        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }

        List<Element> elementList = getElementList(ids);

        Gson gson = new Gson();
        String recommendElementList = gson.toJson(ids);
        Recommend recommend = recommendMapper.selectById(RECOMMENDID);
        recommend.setRecommendElement(recommendElementList);
        recommendMapper.updateById(recommend);

        return elementList;
    }

    @Override
    public List<Element> getElementList(List<Integer> ids) {
        ArrayList<Element> elementList = new ArrayList<>();
        for (Integer id : ids){
            Element element = elementMapper.selectById(id);
            if (element == null){
                throw new ServiceException(ErrorCode.PARAMS_ERROR);
            }
            elementList.add(element);
        }

        return elementList;
    }
}
